/**
 * Created by Mary on 23.02.2015.
 */
$(document).ready(function () {
    $(".product-color").eq(0).data("id", "");
    $(".product-size").eq(0).data("id", "");
    var product;

    $('#product-param').submit(function (event) {

        alert("$('#product-param').submit");



        var sizes = [];
        for (var i = 0; i < $(".product-size").length; i++) {
            if ($(".product-size").eq(i).val() != '') {
                sizes[i] = {
                    id: $(".product-size").eq(i).data("id"),
                    size: $(".product-size").eq(i).val()
                };
            }
            console.log(sizes);
        }
        console.log(sizes);

        var colors = [];
        for (var i = 0; i < $(".product-color").length; i++) {
            if ($(".product-color").eq(i).val() != '') {
                colors[i] = {
                    id: $(".product-color").eq(i).data("id"),
                    color: $(".product-color").eq(i).val(),
                    image: $(".product-img").eq(i).val()
                };
            }
        }
        console.log(colors);

        product = {
            productSizes: sizes,
            productColors: colors,
            name: $("#product-name").val(),
            category: $("#product-category").val(),
            availability: $("#product-availability").val(),
            knitTechnology: $("#product-knit-technology").val()

        };
        console.log(sizes.id);
        alert("");
        $.ajax({
            type: "POST",
            url: cr + "/product/save.action",
            data: {product:product},
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                alert(data);
            }
        });
    });
});